<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Symbol toPrimitive</title>
</head>

<body>
  <script>
    class Foo { }
    let foo = new Foo()
    console.log(3 + foo)
    console.log(3 - foo)

    class Bar {
      constructor() {
        this[Symbol.toPrimitive] = function (hint) {
          switch (hint) {
            case 'number':
              return 3
            case 'string':
              return 'stringBar'
            case 'default':
            default:
              return 'defaultBar'
          }
        }
      }
    }
    let bar = new Bar()
    console.log(3 + bar)
    console.log(3 - bar)
    console.log(String(bar))
  </script>
</body>

</html>